package com.example.hrsystem.workflow.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.model.WorkflowTask;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 任务历史记录适配器
 * 用于在RecyclerView中显示任务历史记录列表
 */
public class TaskHistoryAdapter extends RecyclerView.Adapter<TaskHistoryAdapter.TaskHistoryViewHolder> {
    private List<WorkflowTask> taskHistoryList = new ArrayList<>();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    @NonNull
    @Override
    public TaskHistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_task_history, parent, false);
        return new TaskHistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskHistoryViewHolder holder, int position) {
        WorkflowTask task = taskHistoryList.get(position);
        holder.bind(task);
    }

    @Override
    public int getItemCount() {
        return taskHistoryList.size();
    }

    /**
     * 设置任务历史记录列表数据
     *
     * @param taskHistoryList 任务历史记录列表
     */
    public void setTaskHistoryList(List<WorkflowTask> taskHistoryList) {
        this.taskHistoryList = taskHistoryList;
        notifyDataSetChanged();
    }

    class TaskHistoryViewHolder extends RecyclerView.ViewHolder {
        private TextView taskNameTextView;
        private TextView taskDescriptionTextView;
        private TextView taskStatusTextView;
        private TextView assigneeTextView;
        private TextView createTimeTextView;
        private TextView completeTimeTextView;
        private TextView commentTextView;

        public TaskHistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            taskNameTextView = itemView.findViewById(R.id.text_task_name);
            taskDescriptionTextView = itemView.findViewById(R.id.text_task_description);
            taskStatusTextView = itemView.findViewById(R.id.text_task_status);
            assigneeTextView = itemView.findViewById(R.id.text_assignee);
            createTimeTextView = itemView.findViewById(R.id.text_create_time);
            completeTimeTextView = itemView.findViewById(R.id.text_complete_time);
            commentTextView = itemView.findViewById(R.id.text_comment);
        }

        /**
         * 绑定任务数据到视图
         *
         * @param task 任务对象
         */
        public void bind(WorkflowTask task) {
            taskNameTextView.setText(task.getName());
            taskDescriptionTextView.setText(task.getDescription());
            taskStatusTextView.setText("状态: " + task.getStatus());
            assigneeTextView.setText("处理人: " + task.getAssigneeId());
            createTimeTextView.setText("创建时间: " + dateFormat.format(task.getCreateTime()));
            
            if (task.getCompleteTime() != null) {
                completeTimeTextView.setText("完成时间: " + dateFormat.format(task.getCompleteTime()));
            } else {
                completeTimeTextView.setText("完成时间: ");
            }
            
            // 注意：当前数据模型中没有comment字段，这里暂时显示固定文本
            commentTextView.setText("处理意见: ");
        }
    }
}